import math
import threading
import time
import random
import tkinter as tk
from tkinter import ttk

# 屏幕尺寸 - 先定义默认值
SCREEN_W, SCREEN_H = 1920, 1080
WINDOW_W, WINDOW_H = 120, 60
desired_points = 100  # 心形点数

try:
    root = tk.Tk()
    root.withdraw()
    SCREEN_W = root.winfo_screenwidth()
    SCREEN_H = root.winfo_screenheight()
    root.destroy()
except:
    pass  # 使用默认值


def generate_heart_points(num_points, screen_w, screen_h, window_w, window_h):
    points = []
    center_x = screen_w // 2
    center_y = screen_h // 2
    for i in range(num_points):
        t = i / num_points * 2 * math.pi
        # 心形参数方程
        x = 16 * (pow(math.sin(t), 3))
        y = 13 * math.cos(t) - 5 * math.cos(2 * t) - 2 * math.cos(3 * t) - math.cos(4 * t)
        # 缩放
        scale = min(screen_w // 40, screen_h // 40)
        x = center_x + x * scale
        y = center_y - y * scale  
        # 范围
        x = max(window_w // 2, min(x, screen_w - window_w // 2))
        y = max(window_h // 2, min(y, screen_h - window_h // 2))
        points.append((int(x), int(y)))
    return points


def show_warn_tip(x, y, w, h):
    root = tk.Tk()
    root.overrideredirect(True)
    root.geometry(f"{w}x{h}+{x - w // 2}+{y - h // 2}")
    root.attributes("-topmost", True)

    # 颜色
    r = random.randint(200, 255)
    g = random.randint(200, 255)
    b = random.randint(200, 255)
    root.configure(bg=f'#{r:02x}{g:02x}{b:02x}')

    # 标签
    tips = [
        "保持好心情", "我想你了", "保持微笑", "天天都要元气满满", "别熬夜啦",
        "记得吃水果", "好好吃饭", "拒绝emo！", "保持你的纯真", "每天都要开心",
        "每天都很想你", "你的微笑很特别", "要一直幸福哦", "想你的每一天",
        "照顾好自己", "记得想我", "希望天天见到你", "想你啦"
    ] #可以自定义
    tip = random.choice(tips)
    label = ttk.Label(root, text=tip, font=('仿宋', 10),
                      background=f'#{r:02x}{g:02x}{b:02x}')
    label.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)

    # 关闭窗口
    def close():
        root.destroy()

    root.after(20000, close)  # 20秒后关闭
    root.mainloop()


if __name__ == "__main__":
    points = generate_heart_points(desired_points, SCREEN_W, SCREEN_H, WINDOW_W, WINDOW_H)
    threads = []

    for (x, y) in points:
        t = threading.Thread(target=show_warn_tip, args=(x, y, WINDOW_W, WINDOW_H))
        threads.append(t)
        t.start()
        time.sleep(0.12)  # 每个窗口出现的间隔

    # 等待所有窗口显示完成
    hold_seconds = 12  # 出现后停顿的时间s
    time.sleep(hold_seconds)